<?php

namespace App\Admin\Controllers;

use App\ParamType;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\ParamValue;

class ParamValueController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '参数';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ParamValue());
        $grid->model()->latest();
        if (request('type') > 0) {
            $grid->model()->where('type_id', request('type'));
        }
        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', '参数名称');
            $filter->equal('id', '参数 ID');

        });
        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->text('name', '参数名称');
            $types = ParamType::pluck('name', 'id');
            $create->select('type_id', 'placeholder...')->options($types)->default(request('type'));
        });
        $grid->column('id', __('Param Id'))->sortable();
        $grid->column('name', __('Param Name'))->label()->sortable();
        $grid->column('type_id', __('Param Type'))->display(function ($type) {
            return '<a href="' . admin_url('param-values') . '?type=' . $this->id . '">' . $this->type->id . ' | ' . $this->type->name . '</a>';
        });
        // $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ParamValue::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('type_id', __('Type id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ParamValue());

        $form->text('name', __('Param Name'))->required();
        $form->select('type_id', __('Param Type'))->options(ParamType::pluck('name', 'id'))->required();

        return $form;
    }
}
